package com.willren;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.EditText;

public class GetIpAddressActivity extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		EditText editText = (EditText) findViewById(R.id.editText1);
		if (null != editText) {
			editText.setText(getLocalIpAddress());
		}
	}

	public String getLocalIpAddress() {
		StringBuffer sb = new StringBuffer();
		
		try {
			for (Enumeration<NetworkInterface> en = NetworkInterface
					.getNetworkInterfaces(); en.hasMoreElements();) {
				NetworkInterface intf = en.nextElement();
				for (Enumeration<InetAddress> enumIpAddr = intf
						.getInetAddresses(); enumIpAddr.hasMoreElements();) {
					InetAddress inetAddress = enumIpAddr.nextElement();
					
					sb.append(intf.getName());
					sb.append("[" + inetAddress.isLoopbackAddress() + "]");
					sb.append(" : " + inetAddress.getHostAddress());
					sb.append("\n");
				}
			}
		} catch (SocketException ex) {
			Log.e("WifiPreference IpAddress", ex.toString());
		}
		
		return sb.toString();
	}
}